import { Controller, Post, Body, UnauthorizedException, UseInterceptors } from '@nestjs/common';
import { AuthService } from './auth.service';
import { Public } from '../decorators/public.decorator';
import { TransformDateInterceptor } from '../interceptors/transform-date.interceptor';

@UseInterceptors(TransformDateInterceptor)
@Controller('auth')
export class AuthController {
  constructor(private authService: AuthService) {}

  @Public()
  @Post('login')
  async login(@Body() loginDto: { username: string; password: string }) {
    const user = await this.authService.validateUser(
      loginDto.username,
      loginDto.password,
    );
    
    if (!user) {
      throw new UnauthorizedException('用户名或密码错误');
    }
    const { password, ...userData } = user;
    const { access_token } = await this.authService.login({ id: user.id });
    
    return {
        ...userData,
        access_token
    };
  }
} 